package org.example.springbootdemo.controller;

import org.example.springbootdemo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/return")
//RestController
@Controller
public class ReturnController {
    @RequestMapping("/index")
    public String returnIndex(){
        return "/index.html";
    }
    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData() {
        return "返回视图所需要的数据";
    }
    @RequestMapping("/returnHtml")
    @ResponseBody
    public String returnHtml() {
        return "<h1>Hello, Html </h1>";
    }
    @ResponseBody
    @RequestMapping("/returnJson")
    public Person returnJson() {
        Person person = new Person();
        person.setId(1);
        person.setName("zhangsan");
        person.setAge(19);
        return person;
    }
    @RequestMapping("/returnMap")
    @ResponseBody
    public Map<String, String> returnMap() {
        Map<String, String> kv = new HashMap<>();
        kv.put("k1", "v1");
        kv.put("k2", "v2");
        kv.put("k3", "v3");
        return kv;
    }
    @RequestMapping("/setStatus")
    @ResponseBody
    public String setStatus(HttpServletResponse response) {
        response.setStatus(401);
        return "设置状态码成功";
    }
    @RequestMapping(value = "/r1",produces = "application/json")
    @ResponseBody
    public String r1(HttpServletResponse response) {
        response.setHeader("myhead", "myhead");
        return "{OK : 1}";
    }
}
